package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.List;

@RestController
@RequestMapping("/member")
@CrossOrigin
public class MemberController {


  @Reference
  private MemberService memberService;

    @GetMapping(path = "/findAll")
    public Result findAll(){

        try {
            List <Member> result =  memberService.findAll();

            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);

    }

    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){

        try {
            Member member = memberService.findById(id);

          return   new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  new Result(false,MessageConstant.QUERY_MEMBER_FAIL);

    }




    @GetMapping(path = "/findPage")
    public Result findPage(QueryPageBean queryPageBean) {

        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(),queryPageBean.getQueryString());

            return new Result(true,MessageConstant.QUERY_SETMEALLIST_SUCCESS,pageResult);
        } catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     *
     * @return
     */
    @PostMapping(path = "/add")
    public Result add(@RequestBody Member member ){



        try {
            this.memberService.add(member);

                return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);




        } catch (Exception e) {
            e.printStackTrace();
        }
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);

    }



    @PutMapping(path = "/update")
    public Result update(@RequestBody Member member){

        try {
            memberService.update(member);
            return new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);

    }

    @PutMapping("{id}/{status}")
    public Result updateStatus(@PathVariable(value = "id") Integer id,@PathVariable(value = "status") Integer status){

        try {
            Boolean updateStatus = memberService.updateStatus(id, status);

            if (updateStatus){
                return   new Result(true,MessageConstant.UPDATE_MEMBER_STATUS_SUCCESS);
            }else {
                return  new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new Result(false,MessageConstant.UPDATE_MEMBER_STATUS_FAIL);

    }

}
